package com.ashes.brave.service.impl;

import com.ashes.brave.service.FileSysService;
import lombok.AllArgsConstructor;
import net.sourceforge.tess4j.Tesseract;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @author chengxuhui
 * @description 描述
 */
@Service
@AllArgsConstructor
public class FileSysServiceImpl implements FileSysService {
    private final Tesseract tesseract;

    @Override
    public String uploadFileToOCR(MultipartFile file) {
        StringBuilder stringBuilder = new StringBuilder();
        //对文件进行处理
        try {
            InputStream sbs = new ByteArrayInputStream(file.getBytes());
            BufferedImage bufferedImage = ImageIO.read(sbs);

            //调用ocr处理图片信息
            String ocrStr = tesseract.doOCR(bufferedImage);
            stringBuilder.append(ocrStr);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return stringBuilder.toString();
    }
}
